import 'dart:async';

import 'package:bxultimate/app/theme/gaps.dart';
import 'package:bxultimate/resources/assets.dart';
import 'package:bxultimate/widgets/drag_move_box.dart';
import 'package:bxultimate/widgets/mk_load_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_webrtc/flutter_webrtc.dart';
import 'package:get/get.dart';

import 'controller.dart';

class VideoChatPage extends StatefulWidget {
  const VideoChatPage({super.key});

  @override
  State<VideoChatPage> createState() => _VideoChatPageState();
}

class _VideoChatPageState extends State<VideoChatPage> {
  final controller = Get.find<VideoChatController>();
  final state = Get.find<VideoChatController>().state;

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
        init: VideoChatController(),
        builder: (_) {
          return Scaffold(
            body: state.isAccept ? _buildVideo() : _buildWait(),
          );
        });
  }

  Widget _buildWait() {
    return Container(
      width: double.infinity,
      height: double.infinity,
      color: _getBackgroundColor(),
      child: SafeArea(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.end,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Gaps.vGap32,
            Gaps.vGap32,
            Gaps.vGap32,
            Gaps.vGap32,
            Expanded(
              child: Center(
                child: Column(
                  children: [
                    MkLoadImage(
                      Assets.imagesIcon,
                      width: 80,
                      height: 80,
                    ),
                    Gaps.vGap24,
                    Text(
                      '陈坤',
                      style: TextStyle(color: Colors.white, fontSize: 20),
                    ),
                    Gaps.vGap24,
                    Text(
                      '邀请你视频通话',
                      style: TextStyle(color: Colors.white, fontSize: 14),
                    ),
                  ],
                ),
              ),
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                GestureDetector(
                  onTap: controller.reject,
                  child: MkLoadImage(
                    Assets.imNo,
                    width: 60,
                    height: 60,
                  ),
                ),
                GestureDetector(
                  onTap: controller.accept,
                  child: MkLoadImage(
                    Assets.imAccpet,
                    width: 60,
                    height: 60,
                  ),
                ),
              ],
            ),
            Gaps.vGap32,
            Gaps.vGap32,
          ],
        ),
      ),
    );
  }

  var _hideLockStuff = false;

  Widget _buildVideo() {
    return Container(
      width: double.infinity,
      height: double.infinity,
      color: _getBackgroundColor(),
      child: GestureDetector(
        onTap: _cancelAndRestartLockTimer,
        child: Stack(
          alignment: Alignment.center,
          children: [
            SizedBox(
              width: double.infinity,
              height: double.infinity,
              child: RTCVideoView(controller.remoteRenderer),
            ),
            DragMoveBox(
                child: Container(
              width: 140,
              height: 200,
              decoration: BoxDecoration(
                  color: Colors.black, borderRadius: BorderRadius.circular(10)),
              child: RTCVideoView(controller.localRenderer),
            )),
            Positioned(
                bottom: 50,
                child: AnimatedOpacity(
                  opacity: _hideLockStuff ? 0.0 : 1,
                  duration: const Duration(milliseconds: 300),
                  child: Row(
                    children: [
                      GestureDetector(
                        onTap: controller.rotateVideo,
                        child: MkLoadImage(
                          Assets.imRotate,
                          width: 60,
                          height: 60,
                        ),
                      ),
                      Gaps.hGap32,
                      GestureDetector(
                        onTap: controller.close,
                        child: MkLoadImage(
                          Assets.imClose,
                          width: 60,
                          height: 60,
                        ),
                      ),
                      Gaps.hGap32,
                      GestureDetector(
                        onTap: controller.switchOutput,
                        child: MkLoadImage(
                          Assets.imYsq,
                          width: 60,
                          height: 60,
                        ),
                      ),
                    ],
                  ),
                ))
          ],
        ),
      ),
    );
  }

  Timer? _hideLockTimer;

  /// 开启隐藏锁定定时器
  void _startHideLockTimer() {
    _hideLockTimer?.cancel();
    _hideLockTimer = Timer(const Duration(seconds: 3), () {
      setState(() {
        _hideLockStuff = true;
      });
    });
  }

  /// 取消并重启隐藏锁定定时器
  void _cancelAndRestartLockTimer() {
    _hideLockTimer?.cancel();
    _startHideLockTimer();
    // if (_hideLockStuff == true) {
    //   _startHideTimer();
    // }

    setState(() {
      _hideLockStuff = !_hideLockStuff;
    });
  }

  _getBackgroundColor() {
    return const Color(0xFF444444);
  }
}
